package cn.bdqn.controller;

import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultCode;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Hospitalized;
import cn.bdqn.service.HospitalizedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/HM/hospitalized")
public class HospitalizedController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private HospitalizedService hospitalizedService;

    /**
     * 保存一条住院记录
     *
     * @param bunkId
     * @param registrationId
     * @param recipeId
     * @return
     */
    @PostMapping("/saveHospitalized/")
    public Result<Map<String, Object>> saveHospitalized(
            Integer bunkId
            , String registrationId
            , String recipeId
            , Integer floodId) {
        logger.info("进入了 saveHospitalized(保存一条住院记录) 方法");
        Map<String, Object> data = new HashMap<>();
        try {
            hospitalizedService.save(bunkId, registrationId, recipeId, floodId);
            data.put("message", "保存成功");
            logger.info("保存住院记录成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("saveHospitalized(保存一条住院记录)方法抛出了异常:" + e.getMessage());
            return ResultGenerator.genFailResult("网络异常", ResultCode.INTERNAL_SERVER_ERROR);
        }
        return ResultGenerator.genSuccessResult(data);

    }

    @GetMapping("/getHospitalizedList/{name}/{registrationId}")
    public Result<Map<String, Object>> getHospitalizedList(
            @PathVariable("name") String name,
            @PathVariable("registrationId") String registrationId) {
        logger.info("进入了getHospitalizedList(获取住院信息列表)方法 接收参数name:{},registrationId:{}", name, registrationId);
        Map<String, Object> data = new HashMap<>();
        try {
            List<Hospitalized> hospitalizeds = hospitalizedService.queryByNameAndRegistrationId(name.trim(), registrationId.trim());
            data.put("hospitalizeds", hospitalizeds);
            return ResultGenerator.genSuccessResult(data);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("getHospitalizedList(获取住院信息列表)方法抛出了异常:{}", e.getMessage());
            return ResultGenerator.genFailResult("网络异常", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/discharged/{id}")
    public Result<Map<String, Object>> discharged(@PathVariable("id") Integer id) {
        logger.info("进入了discharged(出院)方法 接收参数id:{}", id);
        Map<String, Object> data = new HashMap<>();
        try {
            hospitalizedService.modifyStatusByID(id);
            data.put("message", "出院成功");
            return ResultGenerator.genSuccessResult(data);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("discharged(出院)方法方法抛出了异常:{}", e.getMessage());
            return ResultGenerator.genFailResult("网络异常", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/getHospitalizedList")
    public Result<Map<String, Object>> getHospitalizedList() {
        logger.info("进入了getHospitalizedList(获取住院信息列表)方法");
        Map<String, Object> data = new HashMap<>();
        try {
            List<Hospitalized> hospitalizeds = hospitalizedService.queryHospitalizedList();
            data.put("hospitalizeds", hospitalizeds);
            return ResultGenerator.genSuccessResult(data);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("进入了getHospitalizedList(获取住院信息列表)方法方法抛出了异常:{}", e.getMessage());
            return ResultGenerator.genFailResult("网络异常", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/getHospitalized/{registrationId}/{name}")
    public Result<Map<String, Object>> getHospitalizedByRegistrationIdAndName(
            @PathVariable("registrationId") String registrationId, @PathVariable("name") String name) {
        logger.info("进入了getHospitalizedByPhoneORName(获取住院信息(根据id或者是name)的方法), registrationId:{}, name: {}", registrationId, name);
        Map<String, Object> data = new HashMap<>();
        try {
            List<Hospitalized> hospitalizeds = hospitalizedService.queryHospitalizedByRegistrationIdAndName(registrationId, name);
            data.put("hospitalizeds", hospitalizeds);
            return ResultGenerator.genSuccessResult(data);
        } catch (Exception e) {
            logger.error("进入了getHospitalizedByPhoneORName(获取住院信息(根据id或者是name)的方法)方法方法抛出了异常:{}", e.getMessage());
            e.printStackTrace();
            return ResultGenerator.genFailResult("网络异常").setCode(ResultCode.INTERNAL_SERVER_ERROR);
        }

    }
}
